package com.hongxun.modules.api.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hongxun.modules.api.entity.BBudgetItemEntity;
import com.hongxun.modules.api.entity.BDeviceEntity;
import com.hongxun.modules.api.service.BBudgetItemService;
import com.hongxun.modules.api.service.BDataDictionaryService;
import com.hongxun.modules.api.service.BDeviceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hongxun.modules.api.entity.BDeviceQuantitiesEntity;
import com.hongxun.modules.api.service.BDeviceQuantitiesService;
import com.hongxun.common.utils.PageUtils;
import com.hongxun.common.utils.Query;
import com.hongxun.common.utils.R;




/**
 * 配电设备工程量表
 * 
 * @author zkcn
 * @email youeremail@163.com
 * @date 2018-07-27 15:52:33
 */
@RestController
@RequestMapping("/api/bdevicequantities")
public class BDeviceQuantitiesController {
	@Autowired
	private BDeviceQuantitiesService bDeviceQuantitiesService;
	@Autowired
	private BDeviceService bDeviceService;
	@Autowired
	private BBudgetItemService bBudgetItemService;
	@Autowired
	private BDataDictionaryService bDataDictionaryService;
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
        String wheres =" 1=1 ";

		if(query.get("dguid")!=null&&!query.get("dguid").toString().equals(""))
		{
			wheres+=" and  d_guid = '" + query.get("dguid").toString()+"' ";
		}
		if(query.get("dQWork")!=null&&!query.get("dQWork").toString().equals(""))
		{
			wheres+=" and  d_q_work = '" + query.get("dQWork").toString()+"' ";
		}
		if(query.get("dQType")!=null&&!query.get("dQType").toString().equals(""))
		{
			wheres+=" and  d_q_type = '" + query.get("dQType").toString()+"' ";
		}
        query.put("filterSql",wheres+" ");
		List<BDeviceQuantitiesEntity> bDeviceQuantitiesList = bDeviceQuantitiesService.queryList(query);
		int total = bDeviceQuantitiesService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(bDeviceQuantitiesList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{guid}")
	public R info(@PathVariable("guid") String guid){
		BDeviceQuantitiesEntity bDeviceQuantities = bDeviceQuantitiesService.queryObject(guid);
		
		return R.ok().put("bDeviceQuantities", bDeviceQuantities);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody BDeviceQuantitiesEntity bDeviceQuantities){
		bDeviceQuantitiesService.save(bDeviceQuantities);
		BDeviceEntity bDeviceEntity = bDeviceService.queryObject(bDeviceQuantities.getDGuid());
		if(bDeviceEntity.getdIshave()==null)
		{
			bDeviceEntity.setdIshave(0);
		}
		bDeviceEntity.setdIshave(bDeviceEntity.getdIshave()+1);
		bDeviceService.update(bDeviceEntity);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody BDeviceQuantitiesEntity bDeviceQuantities){
		bDeviceQuantitiesService.update(bDeviceQuantities);
		
		return R.ok();
	}

	@RequestMapping("/addall")
	public R addall(@RequestBody String dguid) {
		BDeviceEntity bDeviceEntity = bDeviceService.queryObject(dguid);

		if (bDeviceEntity.getDClass() == null || bDeviceEntity.getDClass().equals("")) {
			return R.error("预算项目分类不能为空");
		}
		if (bDeviceEntity.getDItem() == null || bDeviceEntity.getDItem().equals("")) {
			return R.error("预算项目不能为空");
		}
		Map<String, Object> mapaPOther = new HashMap<>();
		String wheres = " 1=1 ";
		wheres += " and  b_i_class = '" + bDeviceEntity.getDClass() + "' ";
		wheres += " and  FIND_IN_SET(b_i_item,'" + bDeviceEntity.getDItem() + "')";

		if (bDeviceEntity.getDQuery() != null && !bDeviceEntity.getDQuery().equals("") && !bDeviceEntity.getDQuery().equals("null")) {
			wheres += " and  b_i_lower <= '" + bDeviceEntity.getDQuery() + "' " + " and  b_i_high > '" + bDeviceEntity.getDQuery() + "' ";
		}
		mapaPOther.put("filterSql", wheres + " ");
		List<BBudgetItemEntity> bBudgetItemList = bBudgetItemService.queryList(mapaPOther);
		for (BBudgetItemEntity bBudgetItemEntity : bBudgetItemList) {
			BDeviceQuantitiesEntity bDeviceQuantitiesEntity= new BDeviceQuantitiesEntity();
			String qzsm="";
			if (bBudgetItemEntity.getBIVoltage()!=null&&!bBudgetItemEntity.getBIVoltage().equals(""))
			{
				String names = bDataDictionaryService.selectvaluebykey("voltage_type",bBudgetItemEntity.getBIVoltage());
				qzsm+="电压等级:"+ names+"   ";
			}
			if( bBudgetItemEntity.getBIParameter()!=null&&!bBudgetItemEntity.getBIParameter().equals(""))
			{
				qzsm += bBudgetItemEntity.getBIParameter() + ":" +bDeviceEntity.getDQuery()+"  ";
			}
        	bDeviceQuantitiesEntity.setDQRatio(new BigDecimal(1));
			bDeviceQuantitiesEntity.setDGuid(dguid);
			bDeviceQuantitiesEntity.setDQType(bBudgetItemEntity.getBIType());
			bDeviceQuantitiesEntity.setDQWork(bBudgetItemEntity.getBIWork());
			bDeviceQuantitiesEntity.setDQPrice(bBudgetItemEntity.getBIPrice());
			bDeviceQuantitiesEntity.setDQMeterUnit(bBudgetItemEntity.getBIUnit());
			String namesmy = bDataDictionaryService.selectvaluebykey("work_type",bBudgetItemEntity.getBIWork());
            bDeviceQuantitiesEntity.setDQQuantities( bBudgetItemEntity.getBIClass()+"-"+ bBudgetItemEntity.getBIItem() +"-"+ namesmy);
			bDeviceQuantitiesEntity.setDQUnit(bDeviceEntity.getDUnit());
			bDeviceQuantitiesEntity.setDQExplain(qzsm);
			bDeviceQuantitiesService.save(bDeviceQuantitiesEntity);


		}
		if(bDeviceEntity.getdIshave()==null)
		{
			bDeviceEntity.setdIshave(0);
		}
		bDeviceEntity.setdIshave(bDeviceEntity.getdIshave()+bBudgetItemList.size());
		bDeviceService.update(bDeviceEntity);

		return R.ok();
	}
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody String[] guids){
		for(String guid :guids ) {
			BDeviceQuantitiesEntity bDeviceQuantities=   bDeviceQuantitiesService.queryObject(guid);
			BDeviceEntity bDeviceEntity = bDeviceService.queryObject(bDeviceQuantities.getDGuid());
			if (bDeviceEntity.getdIshave() == null) {
				bDeviceEntity.setdIshave(0);
			}
			bDeviceEntity.setdIshave((bDeviceEntity.getdIshave() - 1)<0?0:(bDeviceEntity.getdIshave() - 1));

			bDeviceService.update(bDeviceEntity);
		}
		bDeviceQuantitiesService.deleteBatch(guids);
		return R.ok();
	}
	
}
